var CONFIG = require('../../../../common/config');
var CODE = require('../../../../common/code');
var async = require('async');
var moment = require('moment');
var retUtil = require('../util/retUtil');
var rpcFunc = require('../../../routes/rpcSrv/rpcFunc');
var ItbTradeOrderDetailModel = require('../../../../common/mysql/mapping/ItbTradeOrderDetailModel');




var api4backUtil = (function () {
    function api4backUtil() {

    }


    /**
     * 检测订单的文档是否符合规范
     *
     * @author 王俊
     * @date 2018-05-31
     *
     * @param orderIds 订单 id 数组
     * @param callback 回调
     *             error: 查询失败
     *             data: {
     *              // orderId 做 key
     *              "198" : {
     *                      // docId 做 key
     *                      "298" : 0
     *                  }
     *
     *             }
     */
    api4backUtil.checkOrderDocuments = function (orderIds, callback) {

        var totalTasks = [];

        // 1. 查询订单对应的作品数组
        totalTasks.push(function (callback) {

            var entity = new ItbTradeOrderDetailModel();
            entity.getOrdersDetailInfo(orderIds, function (error, details) {
                if(error || !details || details.length <=0) {
                    return callback(error || '订单无效', null);
                }

                var data = {};
                data.orderInfo = {};
                data.totalDocIds = [];

                for (var i=0; i<details.length; i++) {
                    var d = details[i];
                    var docIds = data.orderInfo[d.order_id];
                    if(!docIds) {
                        docIds = [];
                        data.orderInfo[d.order_id] = docIds;
                    }
                    if(d.user_doc_id && d.user_doc_id >0) {
                        docIds.push(d.user_doc_id);
                        data.totalDocIds.push(d.user_doc_id);
                    }
                }
                return callback(error, data);
            });
        });

        // 2. rpc 调用 image 作品
        totalTasks.push(function (data, callback) {

            rpcFunc.yjscCheckDocumentsIsSatisfy(data.totalDocIds, function (error, states) {
                data.totalDocStates = states;
                return callback(error, data);
            })
        });

        // 3. 重组数据
        totalTasks.push(function (data, callback) {
            data.retData = {};
            for (var orderId in data.orderInfo) {

                var docIds = data.orderInfo[orderId];
                var orderResult = data.retData[orderId];
                if(!orderResult) {
                    orderResult = {};
                    data.retData[orderId] = orderResult;
                }

                // 循环 docId 取出对应结果存在 orderResult 中
                for (var i=0; i<docIds.length; i++) {
                    var id = docIds[i];
                    var state = data.totalDocStates[id];
                    orderResult[id] = state;
                }
            }
            return callback(null, data);
        });

        async.waterfall(totalTasks, function (error, data) {
            if(error) {
                return callback(error, null)
            }
            return callback(error, data.retData);
        })

    };





    return api4backUtil;
})();



module.exports = api4backUtil;